package com.rbn.stockmobiledevices.clientes.internal;

import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.transaction.annotation.Transactional;
import com.rbn.stockmobiledevices.clientes.Cliente;
import com.rbn.stockmobiledevices.clientes.ClienteManager;

public class HibernateClienteManager implements ClienteManager {

	private SessionFactory sessionFactory;
	//private Logger logger
	
	public HibernateClienteManager(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public List<Cliente> getAll(){
		return getCurrentSession().createQuery("from Cliente").list();
	}
	
	@Transactional(readOnly = true)
	public Cliente getById(Integer id){
		 Cliente c = (Cliente) getCurrentSession().get(Cliente.class, id);
		 return c;
	}
	
	@Transactional
	public void update(Cliente cliente){
		getCurrentSession().update(cliente);
	}
	
	@Transactional
	public Integer save(Cliente cliente){
		getCurrentSession().save(cliente);
		cliente = getByUnicNombre(cliente.getNombre());
		return cliente.getId();
	}
	
	@Transactional
	public void remove(Cliente cliente){
		getCurrentSession().delete(cliente);
	}
	
	@Transactional
	public boolean remove(Integer id) {
		Cliente c = this.getById(id);
		if(c==null)
			return false;
		getCurrentSession().delete(c);
		return true;
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Cliente> getPaginadoClientes(Integer startPosition, Integer pageSize, String search) {
		Session session = getCurrentSession();
		String hlqQuery = "from Cliente c";
		// hlqQuery += " left join fetch d.trabajadores";

		if (search != null && search.trim().length() > 0) {
			hlqQuery += " where nombre like '%" + search + "%'";
		}
		hlqQuery += " order by id";
		Query q = session.createQuery(hlqQuery);
		
		if(startPosition!=null && pageSize!=null){
			q.setFirstResult(startPosition);
			q.setMaxResults(pageSize);
		}
		return q.list();
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Cliente> getByNombre(String search) {
		Session session = getCurrentSession();
		String hlqQuery = "from Cliente c";

		if (search != null && search.trim().length() > 0) {
			hlqQuery += " where nombre like '%" + search + "%'";
		}
		hlqQuery += " order by id";
		Query q = session.createQuery(hlqQuery);
		
		return q.list();
	}
	
	@Transactional(readOnly = true)
	public Cliente getByUnicNombre(String nombre) {
		Session session = getCurrentSession();
		String hlqQuery = "from Cliente c";
		hlqQuery += " where nombre like :nombre";
		hlqQuery += " order by id";
		Query q = session.createQuery(hlqQuery);
		q.setString("nombre", nombre);
		return (Cliente)q.uniqueResult();
	}
	
	protected Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}
}